<?php

class record implements Iterator
{
	
	private $_valuesOriginal = array(); //private
	private $_values = array(); //private - this is where the values get actually stored.
	
	private $_model; //private - model object
	private $_parent; //private - parent class
	
	private $_isRelative = false;
	
	private $_maxRecursions;
		
	public function __construct($data, $model, $parent=null, $maxRecursions=null)
	{
		// Initially the parent is the same as the model but since we need recursive records
		//   and recordsets the parent can be later set to a recordset or another record.
		$this->_model = $model;
//		$this->setParent($parent ? $parent : $model);
		
		foreach($data as $column=>$value)
		{
			$this->{$column} = $value;
		}
//		show($this->getModel()->recordInstances);
	}
	
	
	// each record is only created once. after that in order to create a new instance we clone the object
	//    and then initialize it in order to create it's relatives and register the new instance.
	public function initialize($parent=null, $maxRecursions=null)
	{
		if($parent)
		{
			$this->setParent($parent);
			$this->_isRelative = true;
		}
//		else
//		{
//			$this->setParent($this->_model);
//		}
		
		$this->_maxRecursions = $maxRecursions;
		if(($this->_maxRecursions>0)||($this->_maxRecursions===null))
		{
			$this->createRelations();
		}
		
		$this->getModel()->recordInstances[] = $this;
	}
	
	public function getModel()
	{
		return $this->_model;
	}
	
	public function getParent()
	{
		return $this->_parent;
	}
	
	public function setParent($parent)
	{
		$this->_parent = $parent;
		return $parent;
	}
	
	public function createRelations()
	{
		// if the parent is a recordset object we should deffer the creation of the relations
		//    after all the records are added to the recordset and let the rs handle them.
		$relations = $this->getModel()->relations;
		
		foreach($relations as $as=>$relation)
		{
			// check if all needed parts of the array are here.
			$value = $this->{$relation['local']};
			$linkedModel = registry::getInstance()->models->{$relation['model']};
			$relativeMaxRecursion = ($this->_maxRecursions>0) ? ($this->_maxRecursions - 1) : ($relation['recursion'] - 1);
			if($relativeMaxRecursion>$relation['recursion'])
			{
				$relativeMaxRecursion = ($relation['recursion'] - 1);
			}
			if($relativeMaxRecursion>=0)
			{
				if($relation['type']=='one')
				{
					$this->{$relation['as']} = $linkedModel->findOneBy($relation['foreign'], $value, $this, $relativeMaxRecursion);
				}
				elseif($relation['type']=='many')
				{
					$this->{$relation['as']} = $linkedModel->findAllBy($relation['foreign'], $value, $this, $relativeMaxRecursion);
					$this->{$relation['as']}->setParentRecord($this);
				}
			}
		}
	}
	
	public function __destruct(){}
	
	public function __set($name, $value)
	{
		if(!array_key_exists($name, $this->_values))
		{
			$this->_valuesOriginal[$name] = $value;
			$this->_values[$name] = $value;
		}
		else
		{
			$this->_values[$name] = $value;
		}
		return true;
	}
	
	public function __get($name)
	{
		if (array_key_exists($name, $this->_values))
		{
			return $this->_values[$name];
		}
	}
	
	public function isDirty()
	{
		foreach($this->_valuesOriginal as $column=>$originalValue)
		{
			if($this->_valuesOriginal[$column]!=$this->_values[$column])
			{
				return true;
			}
		}
		return false;
	}
	
	public function validate()
	{
		//check if all values are valid based on the $_columns
	}
	
	public function save()
	{
		if($this->id)
		{
			if($this->isDirty())
			{
				//validate
				//save changes.
				$model = $this->getModel();
				$db = registry::getInstance()->db;
				
				$data = array();
				foreach($this as $key=>$value)
				{
					$data[$key] = $value;
				}
				
				$db->update($model->tableName, $data, "id='{$this->id}'");
				//reload record?.
			}	
		}
		else
		{
				$model = $this->getModel();
				$db = registry::getInstance()->db;
				
				$data = array();
				foreach($this as $key=>$value)
				{
					$data[$key] = $value;
				}
				
				$db->insert($model->tableName, $data);
		}
	}
	
	public function delete()
	{
		//delete the record and destroy the object (somehow)
	}

	public function count()
	{
		return count($this->_values);
	}

	public function rewind()
	{
		reset($this->_values);
	}
	
	public function current()
	{
		$var = current($this->_values);
		return $var;
	}
	
	public function key()
	{
		$var = key($this->_values);
		return $var;
	}
	
	public function next()
	{
		$var = next($this->_values);
		return $var;
	}
	
	public function valid()
	{
		$var = $this->current() !== false;
		return $var;
	}

}

?>